package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.backend_user.BackEnd_LoginService;
import cn.appsys.tools.Constants;

@Controller
@RequestMapping("/manager")
public class LoginController {

	private Logger log = Logger.getLogger(LoginController.class);

	@Resource(name = "backEnd_LoginService")
	public BackEnd_LoginService BackEnd_LoginService;
	@RequestMapping("/login")
	public String login(){
		return "backendlogin";
	}
	@RequestMapping("/dologin")
	public String dologin(@RequestParam String userCode,
			@RequestParam String userPassword,
			HttpSession session,
			HttpServletRequest request){
		log.debug("=================doLogin=================" + "  userCode: " + userCode + "  userPassword: "
				+ userPassword);
		BackendUser backendUser = BackEnd_LoginService.QueryUserByCode(userCode, userPassword);
		if (null != backendUser) {
			log.info("用户登录,用户名:userCode " + userCode);
			// 登录成功
			// 把用户数据放入Session
			session.setAttribute(Constants.USER_SESSION, backendUser);
			// 跳转
			// return "frame";
			// spring mvc所提供的重定向的功能 ,redirect:url
			return "redirect:main";
		} else {
			request.setAttribute("error", "用户名或者密码错误");
			request.setAttribute("uname", userCode);
			return "backendlogin";
		}
	}
	@RequestMapping("/main")
	public String main()
	{
		return "/backend/main";
	}
	@RequestMapping("logout")
	public String logout(){
		return "backendlogin";
	}
}
